package com.ysstech.finance.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.finance.entity.*;
import com.ysstech.finance.enums.FinanceEnum;
import com.ysstech.finance.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-03-13
 */
@Controller
@Slf4j
@Api(tags = "凭证模板管理api")
@RequestMapping("/voucherTemplate")
public class VoucherTemplateController extends BaseController {

    @Autowired
    private VoucherTemplateService voucherTemplateService;
    @Autowired
    private SubjectProjectService subjectProjectService;

    @YssPermissions(value = {"finance:voucher:template"})
    @ApiOperation(value = "跳转凭证模板管理页面")
    @YssLog(module = "凭证模板管理", remark = "跳转凭证模板管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "finance/voucherTemplate";
    }


    @YssPermissions(value = {"finance:voucher:template"})
    @ApiOperation(value = "获取凭证模板页面初始值")
    @YssLog(module = "凭证模板管理", remark = "获取凭证模板页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init(String projectId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 获取所有凭证来源
        List<Dicvalue> voucherSourceList = dicValueService.getCacheDicValueList(FinanceEnum.VOUCHER_SOURCE.getCode());
        map.put("voucherSourceList", voucherSourceList);
        map.put("voucherSourceList2", voucherSourceList);
        //核算方案
        List<SubjectProject> list = subjectProjectService.list();
        map.put("listProject", list);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    @ApiOperation(value = "获取所有凭证来源下的凭证类型")
    @YssLog(module = "凭证模板管理", remark = "获取所有凭证来源下的凭证类型")
    @PostMapping(value = "/getVoucherType")
    @ResponseBody
    public RestResult getVoucherType(String source) throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 获取所有凭证类型
        List<Dicvalue> voucherTypeList = dicValueService.queryDicValueList(source);
        map.put("voucherTypeList", voucherTypeList);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取凭证模板信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:voucher:template"})
    @ApiOperation(value = "获取凭证模板信息")
    @YssLog(module = "凭证模板管理", remark = "获取凭证模板信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(VoucherTemplate voucherTemplate, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取凭证模板信息成功",
                voucherTemplateService.queryAllVoucherTemplate(voucherTemplate, this.getPage(page, rows)));
    }


    /**
     * 新增凭证模板信息
     *
     * @param voucherTemplate 凭证模板信息
     * @param ids             核算项明细信息编码
     * @throws Exception
     */
    @YssPermissions(value = {"finance:voucher:template:add"})
    @ApiOperation(value = "新增凭证模板信息")
    @YssLog(module = "凭证模板管理", remark = "新增凭证模板信息")
    @RequestMapping(value = "/addVoucherTemplate")
    @ResponseBody
    public RestResult addVoucherTemplate(VoucherTemplate voucherTemplate, String ids) throws Exception {
        voucherTemplateService.addVoucherTemplate(voucherTemplate, ids);
        return ResultGenerator.getSuccessResult("新增角色信息成功！");
    }

    /**
     * 修改凭证模板信息
     *
     * @param voucherTemplate 凭证模板信息
     * @param ids             核算项明细信息编码
     * @throws Exception
     */
    @YssPermissions(value = {"finance:voucher:template:edit"})
    @ApiOperation(value = "修改凭证模板信息")
    @YssLog(module = "凭证模板管理", remark = "修改凭证模板信息")
    @RequestMapping(value = "/editVoucherTemplate")
    @ResponseBody
    public RestResult editVoucherTemplate(VoucherTemplate voucherTemplate, String ids) throws Exception {
        voucherTemplate.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        voucherTemplateService.editVoucherTemplate(voucherTemplate, ids);
        return ResultGenerator.getSuccessResult("修改凭证模板信息成功！");
    }

    /**
     * 删除凭证模板信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:voucher:template:del"})
    @ApiOperation(value = "删除凭证模板信息")
    @YssLog(module = "凭证模板管理", remark = "删除凭证模板信息")
    @RequestMapping(value = "/delVoucherTemplate")
    @ResponseBody
    public RestResult delVoucherTemplate(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除凭证模板信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        voucherTemplateService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除凭证模板信息成功！");
    }
}
